<template>
    <div class="table-box">
        <div class="table-search">
            <el-form :inline="true" :model="form">
                <el-form-item label="用户UID">
                    <el-input v-model="form.uid" placeholder="用户UID" />
                </el-form-item>
                <el-form-item label="事件名称">
                    <el-input v-model="form.event" placeholder="事件名称" />
                </el-form-item>
                <el-form-item label="设备ID">
                    <el-input v-model="form.device_id" placeholder="设备ID" />
                </el-form-item>
                <el-form-item label="时间">
                  <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="截止日期" :default-time="pickerDefaultTime" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button @click="onReset">重置</el-button>
                    <el-button type="primary" plain @click="onExport">导出</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card content-box">
            <div class="table">
                <el-table :data="tableData" border v-loading="loading">
                    <el-table-column prop="id" label="ID" align="center" />
                    <el-table-column prop="createtime" label="记录时间" align="center" :formatter="formatterTime" width="160"/>
                    <el-table-column prop="uid" label="用户UID" align="center" width="100" />
                    <el-table-column prop="channel" label="APP渠道" align="center" />
                    <el-table-column prop="event" label="事件Key" align="center" width="180" />
                    <el-table-column prop="event_value" label="事件值" align="center" width="250" />
                    <el-table-column prop="button" label="按钮" align="center" />
                    <el-table-column prop="description" label="描述" align="center" width="200" />
                    <el-table-column prop="view" label="From" align="center" width="150" />
                    <el-table-column prop="to_view" label="To" align="center" width="100" />
                    <el-table-column prop="source" label="来源" align="center" />
                    <el-table-column prop="device_id" label="设备ID" align="center" />
                    <el-table-column prop="device_type" label="设备类型" align="center" />
                    <el-table-column prop="phone_type" label="手机类型" align="center" />
                    <el-table-column prop="phone_os" label="系统" align="center" />
                    <el-table-column prop="sys_version" label="系统版本" align="center" />
                    <el-table-column prop="sys_name" label="系统名称" align="center" />
                </el-table>
                <div class="table-pagination">
                    <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize"
                        :page-sizes="[10, 25, 50, 100]" :background="true" layout="total, sizes, prev, pager, next, jumper"
                        :total="pageable.total" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"></el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportOrDownload, formatterTime, pickerDefaultTime, timestamp } from "@/utils/utils";
import { statsEventPointApi } from "@/api/moby/stats/stats";

const loading = ref(false);
const tableData = ref([]);
const formData = {
    uid: '',
    event: '',
    export: '',
    device_id: '',
    timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0
});

//参数
const param = () => {
    return {
        page: pageable.pageNum,
        pagesize: pageable.pageSize,
        uid: form.uid,
        event: form.event,
        device_id: form.device_id,
        start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
        end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : ""
    };
};

// 获取表格数据
const getTbaleData = async () => {
    loading.value = true;
    try {
        const res = await statsEventPointApi(param());
        loading.value = false;
        tableData.value = res.data.results;
        pageable.total = res.data.count;
    } catch (error) {
        loading.value = false;
    }
};
//查询
const onQuery = async () => {
    getTbaleData();
};
// 重置
const onReset = () => {
    Object.assign(form, formData);
    getTbaleData();
};
// 导出
const onExport = async () => {
    const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
    const res = await statsEventPointApi(data, "blob");
    exportOrDownload(res, "Metafun-埋点统计");
};

// 条数
const handleSizeChange = number => {
    pageable.pageSize = number;
    getTbaleData();
};

// 页码
const handleCurrentChange = number => {
    pageable.pageNum = number;
    getTbaleData();
};
onMounted(() => {
    getTbaleData();
});
</script>
  